package com.jacky.morelistview;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.google.gson.Gson;
import com.jacky.morelistview.adapter.CommonAdapter;
import com.jacky.morelistview.adapter.ViewHolder;
import com.jacky.morelistview.bean.SpotBean;
import com.jacky.morelistview.widget.MoreListView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    MoreListView vList;

    private List<SpotBean> mDatas = new ArrayList<>();

    private CommonAdapter<SpotBean> mAdapter;
    private String mJson = "{\n" +
            "    \"data\": [\n" +
            "        {\n" +
            "            \"title\": \"荻芦泽畔\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"湖山在望\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"田园童梦\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"全景广场\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"落霞归雁\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"枫多山\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"湖心岛\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"林间探微\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"鹄梦回塘\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"塘野蛙鸣\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"湖山在望\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"田园童梦\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"全景广场\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"落霞归雁\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"枫多山\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"湖心岛\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"林间探微\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"鹄梦回塘\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"塘野蛙鸣\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"湖山在望\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"田园童梦\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"全景广场\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"落霞归雁\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"枫多山\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"湖心岛\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"林间探微\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"鹄梦回塘\"\n" +
            "        },\n" +
            "        {\n" +
            "            \"title\": \"塘野蛙鸣\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"code\": 100,\n" +
            "    \"message\": \"操作成功!\",\n" +
            "    \"isNext\": true\n" +
            "}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vList = (MoreListView) findViewById(R.id.list);
        initAdapter();
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //模拟接口正常返回数据
                vList.setFill(new Gson().fromJson(mJson, SpotBean.class), true);
            }
        });


        vList.setOnloadListener(new MoreListView.OnLoadListener() {
            @Override
            public void onLoadMore() {
                //分页
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        vList.setFill(new Gson().fromJson(mJson, SpotBean.class), false);
                    }
                }, 1000);
            }

            @Override
            public void onRefresh(List datas) {
                //刷新数据源
                mDatas = datas;
                mAdapter.notifyDataSetChanged();
            }
        });
    }

    private void initAdapter() {
        vList.setData(mDatas);
        mAdapter = new CommonAdapter<SpotBean>(this, mDatas, R.layout.item) {
            @Override
            public void convert(ViewHolder holder, SpotBean item, int position) {
                holder.setText(R.id.title, item.getTitle());
            }
        };
        vList.setAdapter(mAdapter);
    }

}
